<?php

namespace App\Http\Controllers\Admin;

use App\Http\Requests\UserSetPasswordRequest;
use App\Repositories\UserRepository;
use App\Utils\ViewHelper;
use Illuminate\Http\Request;
use App\Http\Requests\UserSaveRequest;

/**
 * 控制器
 * Class UserController
 * @package App\Http\Controllers\Admin
 */
class UserController extends BaseController
{
    private $rep;

    public function __construct(UserRepository $rep)
    {
        $this->set('user');
        $this->rep = $rep;
    }

    function index(Request $request)
    {
        $page = $this->rep->adminPager($request->all());
        return $this->view('index',compact('page'));
    }

    function create()
    {
        return $this->view('create');
    }

    function save(UserSaveRequest $request)
    {
        $data = $request->all();
        $id = $request->get('id', 0);
        return $this->rep->save($data, $id);
    }

    function edit($id)
    {
        $result = $this->rep->editInfo($id);
        if ($result->isSuccess()) {
            return $this->view('edit')->with('entity', $result->data);
        }
        return ViewHelper::E404($result->msg);
    }

    function destroy($id)
    {
        return $this->rep->destroy($id);
    }
    function status($status,$id)
    {
        return $this->rep->changeStatus($id, $status);
    }

    function setPasswordView($id)
    {
        return $this->view('setPassword')->with('id', $id);
    }

    function setPassword(UserSetPasswordRequest $request)
    {
        $id = $request->get('id', 0);
        $password = $request->get('password', 0);
        return $this->rep->setPassword($id, $password);
    }
}
